export default {
  data() {
    return {
      page: 1, //分页
      size: 10, //分页长度
      hint: "~上拉加载更多~", //提示语
      // flatList: [], //数据列表
      flatList: [],
      more: true, //是否有更多数据
    };
  },
  onLoad() {
    console.log(12312);
  },
  onReady() {
    // console.log("flatlist=----onReady");
    if (this.state == 1) {
      this.render();
    }
  },
  created() {
    // console.log("flatlist=----");
    if (this.state == 1) {
      this.render();
    }
  },
  watch: {
    state(newVal, oldVal) {
      //监听state 变化，进行列表数据状态操作
      console.log(newVal);
      this.reload(newVal);
    },
  },
  methods: {
    //进行列表相关操作
    reload: function (state) {
      if (state == 1) {
        this.page = 1;
        this.render();
      }
      if (state == 2) {
        console.log(typeof this.flatOpt.index);
        // 列表数据本地删除操作

        if (typeof this.flatOpt.index == "string") {
          //一次性删除多条数据
          let arr = this.flatOpt.index.split(",");
          arr = arr.reverse();
          console.log(arr);
          for (let i in arr) {
            this.flatList.splice(arr[i], 1);
          }
        } else {
          //一次删除一条数据
          this.flatList.splice(this.flatOpt.index, 1);
        }
        this.changeData()
      }
      if (state == 3) {
        // 列表单条数据刷新操作

        this.flatList[this.flatOpt.index] = this.flatOpt.item;
        console.log(this.flatList);
        this.changeData()
      }
      if(state == 4){
        this.render();
      }
      
      //重置状态值
      this.$emit("reloadSuccess", {
        state: 99,
      });
    },
    scrolltolower:function(){
       this.$emit("reloadSuccess", {
        state: 4,
      })
    },
    render: function () {
      // console.log(this.state,this.keyword)
      // this.$emit("reloadSuccess", {
      //   state: 1,
      // })
      if ((this.more) && this.state != 99) {
        let page = {
          page_no: this.page,
          page_size: this.size,
        };

        if (this.keyword) {
          page.keyword = this.keyword;
        }
        return this.$emit("fetch", {
          page,
          success: (data) => {
            return this.success(data);
          },
          // fail: (fail) => {
          //     return this.error(fail);
          // }
        });
      }
    },
    success: function (data) {
      let more;
      if (data instanceof Array) {
        more = data.length < this.size ? false : true;
      } else {
        more = Object.keys(data).length < this.size ? false : true;
      }

      console.log(more, data.length);
      setTimeout(() => {
        this.refTarget = false;
      }, 500);
      // console.log(this.refTarget)
      if (this.page == 1) {
        this.flatList = data;
      } else {
        this.flatList = this.flatList.concat(data);
      }
      this.changeData()
      if (more) {
        console.log(this.page);
        this.page++;
      } else {
        this.more = false;
        this.hint = "~THE END~";
      }
    },

    changeData(){
      return this.$emit("changeData", {
        flatlist: this.flatList,
      });
    }
  },

  beforeDestroy(){
    console.log('beforeDestroy')
    // this.flatList =[];
  },
  destroyed(){
    console.log('destroyed')
    // this.flatList =[];
  }
};